# This script is meant to be run from an analysisSetX directoy. It does the following:
# - calculates the average P2x, P2y, P2z for each z-distance
# - output the file "P2.dat": 1sr colums contains the z-distances, 2nd-3rd-4th columns contain the average P2x P2y P2z

# usage:
# $ python computeAverageP2_singleSet.py 

import sys,string,math,os

o = open('P2.dat','w')  # open output file 

def scanOrdParFileToCalculateAverages( fileName, zDist ):
    f = open( fileName, 'r' )
    lines = f.readlines()
    nLines = 0; # line counter
    p2x = p2y = p2z = 0; # initialise averages

    for line in lines: # loop through lines
        nLines = nLines + 1
        words = string.split( line )
        p2x = p2x + string.atof(words[0]);
        p2y = p2y + string.atof(words[2]);
        p2z = p2z + string.atof(words[4]);

    # print averages
#    print >> o, zDist, p2x / nLines, p2y / nLines, p2z / nLines
    o.write('%-10g % -11.10g % -11.10g % -11.10g\n' % (zDist, p2x / nLines, p2y / nLines, p2z / nLines))
    f.close
    
scanOrdParFileToCalculateAverages('31/ordParSolute.dat', -3.1)
scanOrdParFileToCalculateAverages('29/ordParSolute.dat', -2.9)
scanOrdParFileToCalculateAverages('27/ordParSolute.dat', -2.7)
scanOrdParFileToCalculateAverages('25/ordParSolute.dat', -2.5)
scanOrdParFileToCalculateAverages('23/ordParSolute.dat', -2.3)
scanOrdParFileToCalculateAverages('21/ordParSolute.dat', -2.1)
scanOrdParFileToCalculateAverages('19/ordParSolute.dat', -1.9)
scanOrdParFileToCalculateAverages('17/ordParSolute.dat', -1.7)
scanOrdParFileToCalculateAverages('15/ordParSolute.dat', -1.5)
scanOrdParFileToCalculateAverages('13/ordParSolute.dat', -1.3)
scanOrdParFileToCalculateAverages('11/ordParSolute.dat', -1.1)
scanOrdParFileToCalculateAverages('09/ordParSolute.dat', -0.9)
scanOrdParFileToCalculateAverages('07/ordParSolute.dat', -0.7)
scanOrdParFileToCalculateAverages('05/ordParSolute.dat', -0.5)
scanOrdParFileToCalculateAverages('03/ordParSolute.dat', -0.3)
scanOrdParFileToCalculateAverages('01/ordParSolute.dat', -0.1)

o.close()
